<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Model\ArticleModel;
use App\Model\CommentModel;
use Illuminate\Http\Request;
use Validator;

class ArticleController extends Controller
{

    //文章详情
    public function detail($id) {
        
        $articleModel = new ArticleModel;
        //获取文章
        $article = $articleModel->getArt($id);

        if (isset($article['status']) && $article['status'] == 'fail') {
            return redirect('/');
        }

        //文章阅读数
        $articleModel->viewInc($id);

        //获取点赞最多的文章
        $getLikeArts = $articleModel->getLikeArt();

        //获取上一篇文章
        $preArt = $articleModel->getPreArt($id);
        //获取下一篇文章
        $nextArt = $articleModel->getNextArt($id);

        //获取评论
        $commentModel = new CommentModel;
        $comments = $commentModel->getComments($id);

        return view('home.article.detail',compact('article','getLikeArts','preArt','nextArt','comments'));
    }

    //文章点赞
    public function link($id) {
        //判断用户是否登录
        if ( !session('user') ) {
            return ['status' => 'nologin' , 'msg' => '用户未登录，请先登录'];
        }

        $articleModel = new ArticleModel;
        $username = session('user');

        //判断 此用户是否已经对此文章进行点赞
        if (!$articleModel->checkLike($username , $id)) {
            return ['status' => 'fail' , 'msg' => '你已经点过赞'];
        };
        
        $res = $articleModel->insertLike($username , $id);
        //判断是否点赞成功
        if ($res) {
            return ['status' => 'success' , 'msg' => '点赞成功' , 'num' => $res];
        }
    }

    //文章评论
    public function comment(Request $request) {
        
        //判断用户是否登录
        if ( !session('user') ) {
            return ['status' => 'nologin' , 'msg' => '用户未登录，请先登录'];
        }
        //获取验证码
        $code = trim($request->input('code'));
        $rules = [ 'code' => 'captcha' ];
        $message = ['code.captcha' => '验证码输入不正确'];
        $validator = Validator::make(['code' => $code] , $rules , $message);

        //判断验证码
        if ($validator->fails()) {
            $msg = $validator->errors()->first();
            return [
                'msg' => $msg,
                'status' => 'fail'
            ];
        }

        $username = session('user');
        $time = date('Y-m-d H:i:s');
        $content = trim($request->input('content'));
        $art_id = trim($request->input('art_id'));

        $data = ['username' => $username , 'content' => $content , 'art_id' => $art_id , 'created_at' => $time];

        $commentModel = new CommentModel;
        $res = $commentModel->insertComment($data);

        if ($res) {
            return ['msg' => '评论成功' , 'status' => 'success'];
        } else {
            return ['msg' => '评论失败' , 'status' => 'fail'];
        }

    }

    //文章搜索
    public function search(Request $request) {
        $keywords = $request->input('keywords');
        $articleModel = new ArticleModel;
        $articles = $articleModel->searchArt($keywords);
        
        return view('home.article.search',compact('articles'));
    }
}
